package com.oneonefive.educationsystem.controller;

import com.oneonefive.educationsystem.domain.Lab;
import com.oneonefive.educationsystem.dto.Result;
import com.oneonefive.educationsystem.service.LabService;
import com.oneonefive.educationsystem.vo.LabVo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/lab")
public class LabController {
    @Resource
    LabService labService;

    @PostMapping()
    @PreAuthorize("hasAuthority('admin')")
    public Result labSave(@RequestBody Lab lab) {
        Result legitimacy = labService.legitimacy(lab);
        if (!legitimacy.getSuccess()) {
            return legitimacy;
        }
        boolean success = labService.save(lab);
        if (success) {
            return Result.ok("Lab新增成功");
        }
        return Result.fail("Lab新增失败");
    }

    @DeleteMapping("/{labId}")
    @PreAuthorize("hasAuthority('admin')")
    public Result labDelete(@PathVariable Integer labId) {
        boolean success = labService.removeById(labId);
        if (success) {
            return Result.ok("Lab删除成功");
        }
        return Result.fail("Lab删除失败");
    }

    @PutMapping()
    @PreAuthorize("hasAuthority('admin')")
    public Result labUpdate(@RequestBody Lab lab) {
        Result legitimacy = labService.legitimacy(lab);
        if (!legitimacy.getSuccess()) {
            return legitimacy;
        }
        boolean success = labService.updateById(lab);
        if (success) {
            return Result.ok("Lab修改成功");
        }
        return Result.fail("Lab修改失败");
    }

    //data:List<Lab>
    @PostMapping("/1")
    public Result labList(@RequestBody LabVo labVo) {
        return labService.labList(labVo);
    }

    @DeleteMapping("/allDelete")
    @PreAuthorize("hasAuthority('admin')")
    public Result allDelete(@RequestBody List<Integer> list) {
        for (Integer integer : list) {
            boolean b = labService.removeById(integer);
            if (!b) {
                return Result.fail(integer + "删除失败");
            }
        }
        return Result.ok("批量删除成功");
    }

}

